package me.zzde.wallpaper.Fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import me.zzde.wallpaper.Adapter.MenuAdapter;
import me.zzde.wallpaper.Adapter.WallPaperAdapter;
import me.zzde.wallpaper.R;
import me.zzde.wallpaper.model.Category;
import me.zzde.wallpaper.model.UserWallpaper;

public class CategoryFragment extends Fragment {

    private static String TAG = "CategoryFragment";
    private static String baseUrl = "http://172.20.213.86:8080/";

    private List<String> menuList = new ArrayList<>();
    private List<UserWallpaper> wallpaperList = new ArrayList<>();

    private ListView lv_menu;
    private ListView lv_home;
    private TextView tv_title;

    private View view;

    private MenuAdapter menuAdapter;
    private WallPaperAdapter wallPaperAdapter;

    public CategoryFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        view =  inflater.inflate(R.layout.fragment_category, container, false);
        initView();
        return  view;
    }


    public void loadMenuData(){
        final Gson gson = new Gson();
        OkHttpClient okHttpClient = new OkHttpClient();

        final Request request = new Request.Builder().get().url(baseUrl + "mobile/category").build();

        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Log.i(TAG, "onFailure: " + e);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if (response.isSuccessful()) {
                    try {
                        final String result = response.body().string();
                        List<Category> ca = gson.fromJson(result, new TypeToken<List<Category>>() {}.getType());
                        for (Category c:ca
                                ) {
                            menuList.add(c.getName());
                        }
                        menuAdapter.notifyDataSetInvalidated();
                    } catch (Exception e) {
                        Log.e(TAG, "Exception = " + e);
                    }
                }else{
                    Log.i(TAG, "onResponse: " + "error");
                }
            }
        });
    }

    public void loadWallPaperData(int id){
        final Gson gson = new Gson();
        OkHttpClient okHttpClient = new OkHttpClient();

        final Request request = new Request.Builder().get().url(baseUrl + "mobile/category?id="+id).build();

        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                if (response.isSuccessful()) {
                    try {
                        wallpaperList.clear();
                        final String result = response.body().string();
                        wallpaperList = gson.fromJson(result, new TypeToken<List<UserWallpaper>>() {}.getType());

                        Log.i(TAG, "onResponse: "+ wallpaperList);

                        wallPaperAdapter.notifyDataSetChanged();

                    } catch (Exception e) {
                        Log.e(TAG, "Exception = " + e);
                    }
                }else{
                    Log.i(TAG, "onResponse: " + "error");
                }

            }
        });
    }

    private void initView(){
        lv_menu =  view.findViewById(R.id.lv_menu);
        tv_title =  view.findViewById(R.id.tv_titile);
        lv_home = view.findViewById(R.id.lv_home);

        loadMenuData();
        menuAdapter = new MenuAdapter(getContext(), menuList);
        wallPaperAdapter = new WallPaperAdapter(getContext(), wallpaperList);


        lv_menu.setAdapter(menuAdapter);
        lv_home.setAdapter(wallPaperAdapter);

        lv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                menuAdapter.notifyDataSetChanged();
                loadWallPaperData(i);
                menuAdapter.setSelectItem(i);
                tv_title.setText(menuList.get(i));
                Log.i(TAG, "onItemClick: "+i);
            }
        });
    }
}
